[% setvar title Formats out of core / New format syntax %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Formats out of core / New format syntax</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Wiger &lt;<a href='mailto:nate@wiger.org'>nate@wiger.org</a>&gt;
  Date: 30 Aug 2000
  Last Modified: 16 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 181
  Version: 2
  Status: Retracted</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>There has been much hinting at moving formats out of core. This RFC
proposes one way to do this, at the same time standardizing the syntax.</p>
<a name='NOTES ON RETRACTION'></a><h1>NOTES ON RETRACTION</h1>
<p><b>RFC 230: Replace format built-in with pragmatically-induced format
function</b> is a much more complete and comprehensive proposal which I
personally like better too. ;-)</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='Overview'></a><h2>Overview</h2>
<p>Currently, the general consensus is that formats aren't widely used
enough to justify their living in the core binary. [1] However, they are
incredibly useful, and need to remain in the core distribution no matter
what.</p>
<p>Under this RFC, formats will be available through a simple:</p>
<pre>   use Format;</pre>
<p>The rest of the RFC will address specific changes in syntax to make this
possible (and more consistent too).</p>
<a name='Current Syntax'></a><h2>Current Syntax</h2>
<p>Currently, formats must be named for the bareword filehandle that
they're going to be used on, for example:</p>
<pre>   format FILE =
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;:  @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$name, $ssn
.
   open FILE, &quot;&gt;&gt;$file&quot;;
   write FILE;</pre>
<p>Some behind the scenes magic is done to bind these two together. In
order to change the format name, you have to use the following
statements:</p>
<pre>   $old = select FILE;
   $~ = MYCUSTOMFORMAT;
   select $old;</pre>
<p>Getting this syntax to work as a module would be a collosal headache. In
addition, it's pretty peculiar to begin with. As such, a new but very
similar syntax is proposed.</p>
<a name='New Syntax'></a><h2>New Syntax</h2>
<p>Under the new syntax, a format will be held in a variable of the
<code>format</code> type:</p>
<pre>   my format $FILE_FORMAT = q(
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;:  @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$name, $ssn
);</pre>
<p>Note that declaring the format is remarkably similar to the current
form. However, it now can be handled with the existing variable syntax,
simplifying implementation. [2]</p>
<p>Using formats, however, requires one extra step, since there is no
longer any intrinsic property tying formats and filehandles together:</p>
<pre>   open $FILE, &quot;&gt;&gt;$file&quot;;
   format $FILE ($FILE_FORMAT);   # $FILE-&gt;format($FILE_FORMAT)
   write $FILE;                   # $FILE-&gt;write</pre>
<p>Note, however, that this does get rid of the need to do all the special
<code>select</code> statements. The <code>format</code> and <code>write</code> methods could simply be
member functions of the <code>$FILE</code> object.</p>
<p>However, I don't particularly like extra steps, personally. One way
around this would be to assume the default format is <code>[handle]_FORMAT</code>,
meaning <code>$FILE</code>'s default format would be <code>$FILE_FORMAT</code>. This would
make behavior very similar to current, and would make the above extra
step unnecessary. More clever ways of doing this probably exist as well.</p>
<p>Finally, note that if <i><a href='#RFC 174'>&quot;RFC 174&quot;</a></i> is adopted, this can be made to look
even more simple and consistent:</p>
<pre>   open $FILE, &quot;&gt;&gt;$file&quot;;
   format $FILE, $FILE_FORMAT;    # $FILE-&gt;format($FILE_FORMAT)
   write $FILE;                   # $FILE-&gt;write</pre>
<p>Since the indirect object and function syntaxes could be used
interchangeably.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Hold on.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>There is a need for migration, but I'd rather save this until later
since this idea may get massively revised.</p>
<a name='NOTES'></a><h1>NOTES</h1>
<p>[1] I personally disagree, but this RFC is close enough to make me
happy. :-)</p>
<p>[2] We might consider making a special case in the Perl parser so that
if a variable is declared of type <code>format</code> then the Perl 5 syntax can
be used:</p>
<pre>   my format $FILE_FORMAT =
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;:  @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$name, $ssn
.</pre>
<p>Then this is even less different and scary. Get rid of that <code>my</code> and
it's Perl 5.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 174 (v1): Parse <code>func($obj, @args)</code> as <code>func $obj (@args)</code></p>
</div>
